﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using System.Data;

namespace PMS.Web
{
    public partial class FinanceMaintainace : ExportFiles
    {
        PMS_ProjectMaintainFinanceBLL bll = new PMS_ProjectMaintainFinanceBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string role = PageComm.GetUser(this.Response).role;
                if (role != "管理员")
                {
                    btn_AddFinanceMaintain.Visible = false;
                }
                string projectID = Request["projectID"];
                if (!string.IsNullOrEmpty(projectID))
                {
                    hid_ProjectID.Value = projectID;
                    ProjectLoad();
                    DataLoad();
                }
            }
        }

        /// <summary>
        /// 加载项目信息
        /// </summary>
        private void ProjectLoad()
        {
            string projectID = hid_ProjectID.Value;
            PMS_ProjectBLL pbll = new PMS_ProjectBLL();
            PMS_Project project = pbll.GetModel(Convert.ToInt32(projectID));
            lab_ProjectID.Text = project.projectID;
            lab_JDEID.Text = project.JDEID;
            lab_Customer.Text = project.customer;
            lab_CustomerTel.Text = project.customerTel;
            lab_ProjectName.Text = project.projectName;
            lab_ProjectType.Text = project.projectType;
            lab_SystemPath.Text = project.systemPath;
            lab_City.Text = project.city;
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private PMS_ProjectMaintainFinanceQuery GetQuery()
        {
            PMS_ProjectMaintainFinanceQuery query = new PMS_ProjectMaintainFinanceQuery();
            query.projectID = Convert.ToInt32(hid_ProjectID.Value);
            if (!string.IsNullOrEmpty(txt_FromDate.Text.Trim()))
                query.startDate = Convert.ToDateTime(txt_FromDate.Text.Trim());
            if (!string.IsNullOrEmpty(txt_ToDate.Text.Trim()))
                query.endDate = Convert.ToDateTime(txt_ToDate.Text.Trim());
            return query;
        }

        /// <summary>
        /// 列表绑定
        /// </summary>
        /// <param name="projectID"></param>
        private void DataLoad()
        {
            try
            {
                PMS_ProjectMaintainFinanceQuery query = GetQuery();
                List<PMS_ProjectMaintainFinance> liste = bll.GetList(query);
                gv_PMS_ProjectMaintainFinance.DataSource = liste;
                gv_PMS_ProjectMaintainFinance.DataBind();
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// 加载当前记录
        /// </summary>
        /// <param name="id"></param>
        public void LoadEditText(PMS_ProjectMaintainFinance model)
        {
            hid_ID.Value = model.id.ToString();
            txt_applyStage.Text = model.applyStage;
            txt_balanceMoney.Text = model.balanceMoney == null ? "" : model.balanceMoney.Value.ToString("0.00");
            txt_ChangeAmount.Text = model.changeAmount == null ? "" : model.changeAmount.Value.ToString("0.00");
            txt_claimMoney.Text = model.claimMoney == null ? "" : model.claimMoney.Value.ToString("0.00");
            txt_EndDate.Text = model.endDate == null ? "" : model.endDate.Value.ToString("yyyy-MM-dd");
            txt_OrderAmount.Text = model.orderAmount == null ? "" : model.orderAmount.Value.ToString("0.00");
            txt_paidMoney.Text = model.paidMoney == null ? "" : model.paidMoney.Value.ToString("0.00");
            txt_SettlementAmount.Text = model.settlementAmount == null ? "" : model.settlementAmount.Value.ToString("0.00");
            txt_Remark.Text = model.remark;
            txt_StartDate.Text = model.startDate == null ? "" : model.startDate.Value.ToString("yyyy-MM-dd");
            txt_systemACSAmount.Text = model.systemACSAmount == null ? "" : model.systemACSAmount.Value.ToString("0.00");
            txt_systemACSCount.Text = model.systemACSCount == null ? "" : model.systemACSCount.Value.ToString();
            txt_systemALARMAmount.Text = model.systemALARMAmount == null ? "" : model.systemALARMAmount.Value.ToString("0.00");
            txt_systemALARMCount.Text = model.systemALARMCount == null ? "" : model.systemALARMCount.Value.ToString();
            txt_systemATMAmount.Text = model.systemATMAmount == null ? "" : model.systemATMAmount.Value.ToString("0.00");
            txt_systemATMCount.Text = model.systemATMCount == null ? "" : model.systemATMCount.Value.ToString();
            txt_systemCCTVAmount.Text = model.systemCCTVAmount == null ? "" : model.systemCCTVAmount.Value.ToString("0.00");
            txt_systemCCTVCount.Text = model.systemCCTVCount == null ? "" : model.systemCCTVCount.Value.ToString();
            this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>showContentDiv(1);</script>");
        }

        /// <summary>
        /// 清空
        /// </summary>
        public void ClearText()
        {
            hid_ID.Value = "0";
            txt_applyStage.Text = "";
            txt_balanceMoney.Text = "";
            txt_ChangeAmount.Text = "";
            txt_claimMoney.Text = "";
            txt_EndDate.Text = "";
            txt_OrderAmount.Text = "";
            txt_paidMoney.Text = "";
            txt_SettlementAmount.Text = "";
            txt_Remark.Text = "";
            txt_StartDate.Text = "";
            txt_systemACSAmount.Text = "";
            txt_systemACSCount.Text = "";
            txt_systemALARMAmount.Text = "";
            txt_systemALARMCount.Text = "";
            txt_systemATMAmount.Text = "";
            txt_systemATMCount.Text = "";
            txt_systemCCTVAmount.Text = "";
            txt_systemCCTVCount.Text = "";
        }

        protected void btn_AddFinanceMaintain_Click(object sender, EventArgs e)
        {
            PMS_ProjectMaintainFinance model = new PMS_ProjectMaintainFinance();
            model.projectID = hid_ProjectID.Value;
            model.id = Convert.ToInt32(hid_ID.Value);
            model.customerName = lab_ProjectName.Text.Trim();
            model.applyStage = txt_applyStage.Text.Trim();
            try
            {
                if (!string.IsNullOrEmpty(txt_balanceMoney.Text.Trim()))
                    model.balanceMoney = Convert.ToDecimal(txt_balanceMoney.Text.Trim());
                if (!string.IsNullOrEmpty(txt_ChangeAmount.Text.Trim()))
                    model.changeAmount = Convert.ToDecimal(txt_ChangeAmount.Text.Trim());
                if (!string.IsNullOrEmpty(txt_claimMoney.Text.Trim()))
                    model.claimMoney = Convert.ToDecimal(txt_claimMoney.Text.Trim());
                if (!string.IsNullOrEmpty(txt_EndDate.Text.Trim()))
                    model.endDate = Convert.ToDateTime(txt_EndDate.Text.Trim());
                if (!string.IsNullOrEmpty(txt_OrderAmount.Text.Trim()))
                    model.orderAmount = Convert.ToDecimal(txt_OrderAmount.Text.Trim());
                if (!string.IsNullOrEmpty(txt_paidMoney.Text.Trim()))
                    model.paidMoney = Convert.ToDecimal(txt_paidMoney.Text.Trim());
                if (!string.IsNullOrEmpty(txt_ChangeAmount.Text.Trim()))
                    model.settlementAmount = Convert.ToDecimal(txt_ChangeAmount.Text.Trim());
                model.remark = txt_Remark.Text.Trim();
                if (!string.IsNullOrEmpty(txt_StartDate.Text.Trim()))
                    model.startDate = Convert.ToDateTime(txt_StartDate.Text.Trim());
                if (!string.IsNullOrEmpty(txt_systemACSAmount.Text.Trim()))
                    model.systemACSAmount = Convert.ToDecimal(txt_systemACSAmount.Text.Trim());
                if (!string.IsNullOrEmpty(txt_systemACSCount.Text.Trim()))
                    model.systemACSCount = Convert.ToInt32(txt_systemACSCount.Text.Trim());
                if (!string.IsNullOrEmpty(txt_systemALARMAmount.Text.Trim()))
                    model.systemALARMAmount = Convert.ToDecimal(txt_systemALARMAmount.Text.Trim());
                if (!string.IsNullOrEmpty(txt_systemALARMCount.Text.Trim()))
                    model.systemALARMCount = Convert.ToInt32(txt_systemALARMCount.Text.Trim());
                if (!string.IsNullOrEmpty(txt_systemATMAmount.Text.Trim()))
                    model.systemATMAmount = Convert.ToDecimal(txt_systemATMAmount.Text.Trim());
                if (!string.IsNullOrEmpty(txt_systemATMCount.Text.Trim()))
                    model.systemATMCount = Convert.ToInt32(txt_systemATMCount.Text.Trim());
                if (!string.IsNullOrEmpty(txt_systemCCTVAmount.Text.Trim()))
                    model.systemCCTVAmount = Convert.ToDecimal(txt_systemCCTVAmount.Text.Trim());
                if (!string.IsNullOrEmpty(txt_systemCCTVCount.Text.Trim()))
                    model.systemCCTVCount = Convert.ToInt32(txt_systemCCTVCount.Text.Trim());
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('数据有误，请检查数据是否正确');</script>");
                return;
            }
            if (!bll.AddUpdate(model))
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('更新失败，请稍后重试');</script>");
                return;
            }
            else
            {
                ClearText();
                DataLoad();
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>showContentDiv(0);</script>");
            }
        }

        protected void gv_PMS_ProjectMaintainFinance_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edited")
            {
                if (e.CommandArgument != null)
                {
                    PMS_ProjectMaintainFinance model = bll.GetModel(Convert.ToInt32(e.CommandArgument));
                    LoadEditText(model);
                }
            }
            else if (e.CommandName == "Del")
            {
                if (e.CommandArgument != null)
                {
                    if (bll.Delete(Convert.ToInt32(e.CommandArgument)))
                    {
                        this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('删除成功');</script>");
                        DataBind();
                    }
                    else
                    {
                        this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('删除失败');</script>");
                    }
                }
            }
        }

        protected void gv_PMS_ProjectMaintainFinance_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void btn_Export_Click(object sender, EventArgs e)
        {
            PMS_ProjectMaintainFinanceQuery query = GetQuery();
            DataTable dt = bll.GetListTable(query);
            if (dt != null && dt.Rows.Count > 0)
            {
                ExportExcel("维保项目财务", dt);
            }
        }

        protected void btn_Search_Click(object sender, EventArgs e)
        {
            DataLoad();
        }
    }
}